package com.financial.service.heemoney.service.impl;

import com.financial.service.heemoney.constants.FinancialRegisterHeemoneylogEnumConstants;
import com.financial.service.heemoney.constants.FinancialWxconfigHeemoneylogEnumConstants;
import com.financial.service.heemoney.convertor.HeemoneyRegisterConvert;
import com.financial.service.heemoney.convertor.HeemoneyWxPayConfigConvert;
import com.financial.service.heemoney.dao.FinancialWxconfigHeemoneylogDao;
import com.financial.service.heemoney.domain.FinancialRegisterHeemoneylogDO;
import com.financial.service.heemoney.domain.FinancialWxconfigHeemoneylogDO;
import com.financial.service.heemoney.dto.HeemoneyConfigDTO;
import com.financial.service.heemoney.dto.businsess.HeemoneyRegisterBusinessDTO;
import com.financial.service.heemoney.dto.businsess.HeemoneyWxPayConfigBusinessDTO;
import com.financial.service.heemoney.dto.commmon.HeemoneyRegisterCommonDTO;
import com.financial.service.heemoney.dto.commmon.HeemoneyWxPayConfigCommonDTO;
import com.financial.service.heemoney.dto.inparameter.HeemoneyWxPayConfigInParamDTO;
import com.financial.service.heemoney.dto.param.HeemoneyRegisterParamDTO;
import com.financial.service.heemoney.dto.param.HeemoneyWxPayConfigParamDTO;
import com.financial.service.heemoney.service.FinancialWxconfigHeemoneylogService;
import com.financial.service.heemoney.utils.JsonUtils;
import com.financial.service.heemoney.utils.OkHttpUtils;
import com.financial.service.heemoney.utils.ResultUtils;
import com.financial.service.heemoney.utils.apiresult.ApiResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;


@Service
public class FinancialWxconfigHeemoneylogServiceImpl implements FinancialWxconfigHeemoneylogService {
    @Autowired
    private FinancialWxconfigHeemoneylogDao financialWxconfigHeemoneylogDao;
    @Autowired
    private OkHttpUtils okHttpUtils;

    public static final Logger log = LoggerFactory.getLogger(FinancialWxconfigHeemoneylogServiceImpl.class);

    @Override
    public ApiResult wxconfig(HeemoneyWxPayConfigInParamDTO heemoneyWxPayConfigInParamDTO) {
        log.info("调用金融中心汇收银服务层微信通道配置接口前参数打印：{}", JsonUtils.beanToJson(heemoneyWxPayConfigInParamDTO));
        //请求参数日志保存
        FinancialWxconfigHeemoneylogDO wxconfigHeemoneylogDO = this.saveRequest(heemoneyWxPayConfigInParamDTO);
        //数据转实体校验参数非空
        HeemoneyWxPayConfigCommonDTO heemoneyWxconfigCommonDTO = HeemoneyWxPayConfigConvert.MAPPER.configDTOConvertCommonDTO(new HeemoneyConfigDTO(), heemoneyWxPayConfigInParamDTO);
        HeemoneyWxPayConfigBusinessDTO heemoneyWxconfigBusinessDTO = HeemoneyWxPayConfigConvert.MAPPER.configDTOConvertBusinessDTO(new HeemoneyConfigDTO(), heemoneyWxPayConfigInParamDTO);
        HeemoneyWxPayConfigParamDTO heemoneyWxPayConfigParamDTO = new HeemoneyWxPayConfigParamDTO(heemoneyWxconfigCommonDTO, heemoneyWxconfigBusinessDTO);
        log.info("http请求汇收银服务层微信通道配置接口入参：{}", heemoneyWxPayConfigParamDTO.getJsonParam());
        try {
            String resultStr = okHttpUtils.postJsonParams(new HeemoneyConfigDTO().getHeemoneyWxPayConfig().getRequest_url(), heemoneyWxPayConfigParamDTO.getJsonParam());
            log.info("http请求汇收银服务层微信通道配置接口回执:{}", resultStr);
            //修改注册日志信息表
            this.updateWxPayConfigInfo(resultStr, wxconfigHeemoneylogDO);
            //返回调用信息
            return ResultUtils.resultStr(resultStr);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("http请求汇收银服务层微信通道配置接口失败：{}", e.getMessage());
        }
        return ApiResult.failure();

    }

    private void updateWxPayConfigInfo(String resultStr, FinancialWxconfigHeemoneylogDO wxconfigHeemoneylogDO) {
        Object temp = "";
        Map<String, Object> strMap = JsonUtils.jsonToMap(resultStr);
        String result_code = strMap.get("result_code").toString();
        try {
            wxconfigHeemoneylogDO.setGmtModified(new Date());
            if (!result_code.equals("SUCCESS")) {
                wxconfigHeemoneylogDO.setReturnMessage(strMap.get("error_msg").toString());
                wxconfigHeemoneylogDO.setState(FinancialRegisterHeemoneylogEnumConstants.ENUM_STATE_FAIL);
            } else {
                wxconfigHeemoneylogDO.setReturnMessage(strMap.get("result_msg").toString());
                wxconfigHeemoneylogDO.setState(FinancialRegisterHeemoneylogEnumConstants.ENUM_STATE_SUC);
            }
            financialWxconfigHeemoneylogDao.update(wxconfigHeemoneylogDO);
        } catch (Exception e) {
            log.error("平台商户号：{},修改汇收银微信通道配置信息失败！:{}", wxconfigHeemoneylogDO.getStoreNo(), strMap.get("error_msg"));
            e.printStackTrace();
        }
    }

    private FinancialWxconfigHeemoneylogDO saveRequest(HeemoneyWxPayConfigInParamDTO heemoneyWxPayConfigInParamDTO) {
        FinancialWxconfigHeemoneylogDO wxconfigHeemoneylogDO = new FinancialWxconfigHeemoneylogDO();
        try {
            BeanUtils.copyProperties(heemoneyWxPayConfigInParamDTO, wxconfigHeemoneylogDO);
            wxconfigHeemoneylogDO.setGmtCreate(new Date());
            wxconfigHeemoneylogDO.setGmtModified(new Date());
            wxconfigHeemoneylogDO.setState(FinancialWxconfigHeemoneylogEnumConstants.ENUM_STATE_WAIT);
            wxconfigHeemoneylogDO.setIsDelete(FinancialWxconfigHeemoneylogEnumConstants.ENUM_ISDELETE_UNDEL);
            financialWxconfigHeemoneylogDao.save(wxconfigHeemoneylogDO);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("请求汇收银微信通道配置参数日志保存失败！平台商户号为:{}", heemoneyWxPayConfigInParamDTO.getStore_no());
        }

        return wxconfigHeemoneylogDO;
    }

    @Override
    public FinancialWxconfigHeemoneylogDO get(Integer id) {
        return financialWxconfigHeemoneylogDao.get(id);
    }

    @Override
    public List<FinancialWxconfigHeemoneylogDO> list(Map<String, Object> map) {
        return financialWxconfigHeemoneylogDao.list(map);
    }

    @Override
    public int count(Map<String, Object> map) {
        return financialWxconfigHeemoneylogDao.count(map);
    }

    @Override
    public int save(FinancialWxconfigHeemoneylogDO financialWxconfigHeemoneylog) {
        return financialWxconfigHeemoneylogDao.save(financialWxconfigHeemoneylog);
    }

    @Override
    public int update(FinancialWxconfigHeemoneylogDO financialWxconfigHeemoneylog) {
        return financialWxconfigHeemoneylogDao.update(financialWxconfigHeemoneylog);
    }

    @Override
    public int remove(Integer id) {
        return financialWxconfigHeemoneylogDao.remove(id);
    }

    @Override
    public int batchRemove(Integer[] ids) {
        return financialWxconfigHeemoneylogDao.batchRemove(ids);
    }


}
